package br.ufpe.cin.camid.core.lifecycle.factory;

import br.ufpe.cin.camid.core.objects.AcquireStrategy;
import br.ufpe.cin.camid.core.objects.ManagedObjectHandler;

import com.google.inject.ConfigurationException;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.ProvisionException;

public class SimpleManagedObjectFactory implements ManagedObjectFactory
{
	
	@Inject
	private Injector injector;
	
	
	@Override
	public ManagedObjectHandler createObjectHandlerFromDefinition(
			String objectId,
			Class<?> managedObjectDefinition,
			AcquireStrategy acquireStrategy) 
	{
		
		ManagedObjectHandler handler = null;
		
		try 
		{
			Object instance = injector.getInstance(managedObjectDefinition);
			handler = new ManagedObjectHandler(objectId, instance, acquireStrategy );
		}
		catch(ConfigurationException e)
		{
			throw new ManagedObjectCreationException("Injector configuration error", e);
		}
		catch(ProvisionException e)
		{
			throw new ManagedObjectCreationException("Instance creation error", e);
		}
		
		return handler;
	}

	@Override
	public ManagedObjectHandler createObjectHandlerFromInstance(
			String objectId, Object object, AcquireStrategy acquireStrategy) {
		ManagedObjectHandler handler = new ManagedObjectHandler(objectId, object, acquireStrategy );
		return handler;
	}
	
	
}
